/*!
 * SPDX-License-Identifier: Apache-2.0
 *
 * The OpenSearch Contributors require contributions made to
 * this file be licensed under the Apache-2.0 license or a
 * compatible open source license.
 *
 * Modifications Copyright OpenSearch Contributors. See
 * GitHub history for details.
 */

/**
 * 1. Fix for IE where the image correctly resizes in width but doesn't collapse its height
      (https://github.com/philipwalton/flexbugs/issues/75#issuecomment-134702421)
 */

// Main <figure> that wraps images.
.ouiImage {
  display: inline-block;
  max-width: 100%;
  position: relative;
  min-height: 1px; /* 1 */
  line-height: 0; // Fixes cropping when image is resized by forcing its height to be determined by the image not line-height
  flex-shrink: 0; // Don't ever let this shrink in height if direct descendent of flex

  // Required for common usage of nesting within OuiText
  .ouiImage__img {
    margin-bottom: 0;
  }

  &.ouiImage--hasShadow {
    .ouiImage__img {
      @include ouiBottomShadowMedium;
    }
  }

  .ouiImage__button {
    position: relative;
    cursor: pointer;

    // transition the shadow
    transition: all $ouiAnimSpeedFast $ouiAnimSlightResistance;

    &:focus  {
      outline: 2px solid $ouiFocusRingColor;
    }

    &:hover .ouiImage__icon {
      visibility: visible;
      fill-opacity: 1;
    }

    &--fullWidth {
      width: 100%;
    }
  }

  &.ouiImage--allowFullScreen {
    &:hover .ouiImage__caption {
      text-decoration: underline;
    }

    &:not(.ouiImage--hasShadow) .ouiImage__button:hover,
    &:not(.ouiImage--hasShadow) .ouiImage__button:focus {
      @include ouiBottomShadowMedium;
    }

    &.ouiImage--hasShadow .ouiImage__button:hover,
    &.ouiImage--hasShadow .ouiImage__button:focus {
      @include ouiBottomShadow;
    }
  }

  // These sizes are mostly suggestions. Don't look too hard for meaning in their values.
  // Size is applied to the image, rather than the figure to work better with floats
  &.ouiImage--small .ouiImage__img {
    width: convertToRem(120px);
  }

  &.ouiImage--medium .ouiImage__img {
    width: convertToRem(200px);
  }

  &.ouiImage--large .ouiImage__img {
    width: convertToRem(360px);
  }

  &.ouiImage--xlarge .ouiImage__img {
    width: convertToRem(600px);
  }

  &.ouiImage--fullWidth {
    width: 100%;
  }

  &.ouiImage--original {
    .ouiImage__img {
      width: auto;
      max-width: 100%;
    }
  }

  &.ouiImage--floatLeft {
    float: left;

    &[class*='ouiImage--margin']  {
      margin-left: 0;
      margin-top: 0;
    }
  }

  &.ouiImage--floatRight {
    float: right;

    &[class*='ouiImage--margin']  {
      margin-right: 0;
      margin-top: 0;
    }
  }

  &.ouiImage--marginSmall {
    margin: $ouiSizeS;
  }

  &.ouiImage--marginMedium {
    margin: $ouiSize;
  }

  &.ouiImage--marginLarge {
    margin: $ouiSizeL;
  }

  &.ouiImage--marginXlarge {
    margin: $ouiSizeXL;
  }
}

// The image itself is full width within the container.
.ouiImage__img {
  width: 100%;
  vertical-align: middle;
}

.ouiImage__caption {
  @include ouiFontSizeS;
  margin-top: $ouiSizeXS;
  text-align: center;
}

.ouiImage__icon {
  visibility: hidden;
  fill-opacity: 0;
  position: absolute;
  right: $ouiSize;
  top: $ouiSize;
  transition: fill-opacity $ouiAnimSpeedSlow $ouiAnimSlightResistance;
  cursor: pointer;
}

// The FullScreen image that optionally pops up on click.
.ouiImage-isFullScreen {
  position: relative;
  max-height: 80vh;
  max-width: 80vw;
  animation: ouiImageFullScreen $ouiAnimSpeedExtraSlow $ouiAnimSlightBounce;

  &:hover {
    .ouiImage__button {
      @include ouiBottomShadow;
    }

    .ouiImage__caption {
      text-decoration: underline;
    }
  }

  &__img {
    max-height: 80vh;
    max-width: 80vw;
    vertical-align: middle;
    cursor: pointer;
    transition: all $ouiAnimSpeedFast $ouiAnimSlightResistance;
  }
}

.ouiImage-isFullScreenCloseIcon {
  position: absolute;
  right: $ouiSize;
  top: $ouiSize;
  pointer-events: none;
}

@keyframes ouiImageFullScreen {
  0% {
    opacity: 0;
    transform: translateY($ouiSizeXL * 2);
  }

  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

@include ouiBreakpoint('xs', 's', 'm') {

  .ouiImage {

    &.ouiImage--floatLeft,
    &.ouiImage--floatRight {
      float: none;

      // Return back to whatever margin settings were set without the float
      &[class*='ouiImage--margin']  {
        margin-top: inherit;
        margin-right: inherit;
        margin-bottom: inherit;
        margin-left: inherit;
      }
    }
  }
}
